package com.softlab.retailwholesalesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.RoleEntity;
import com.softlab.retailwholesalesystem.service.IRoleService;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController extends BaseController{

    @Autowired
    IRoleService service;

    @GetMapping
    public JsonResult<List<RoleEntity>> getRoles(){
        QueryWrapper<RoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<RoleEntity> list = service.list(wrapper);
        return new JsonResult<>(200,"获得列表成功",list);
    }

    @GetMapping("/search")
    public JsonResult<List<RoleEntity>> selectList(RoleEntity role){
        List<RoleEntity> list = service.select(role);
        return new JsonResult<>(200,"搜索成功",list);
    }

    @PutMapping
    public JsonResult<Void> updateRole(@RequestBody RoleEntity role){
        boolean b = service.updateById(role);
        if (b){
            return new JsonResult<>(200);
        }
        return new JsonResult<>(500,"更新时遇到未知错误");
    }

    @DeleteMapping("{id}")
    public JsonResult<Void> deleteRole(@PathVariable int id){
        RoleEntity role=new RoleEntity();
        role.setRoleId(id);
        role.setIsDelete(1);
        service.updateById(role);
        return new JsonResult<>(200);
    }

    @PostMapping
    public JsonResult<Void> addRole(@RequestBody RoleEntity role){
        service.save(role);
        return new JsonResult<>(200,"增加成功");
    }

}
